package org.dawnsql.stock.util;

import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;

public class MyJs {

    private static String path = MyScript.class.getResource("").getPath();
    private static int index = path.lastIndexOf("/org/dawnsql/stock/util/");
    private static String line = path.substring(0, index) + "/static/";

    public static String getFileString(String file_path)
    {
        try {
            Path path = Paths.get(new URI("file://" + file_path));
            String sourceCode = new String(Files.readAllBytes(path));
            return sourceCode;
        } catch (URISyntaxException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return "";
    }

    public static String getCode(String codeName)
    {
        return getFileString(line + "/" + codeName);
    }

    public static String getJs(String code)
    {
        StringBuilder sb = new StringBuilder();
        sb.append("datafeed: new e0(\""+ code +"\")");
        return sb.toString();
    }
}
